home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************************
- *
- * Copyright (c) 1993 Silicon Graphics, Inc.
- * All Rights Reserved
- *
- * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
- *
- * The copyright notice above does not evidence any actual of intended
- * publication of such source code, and is an unpublished work by Silicon
- * Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
- * the property of Silicon Graphics, Inc. Any use, duplication or
- * disclosure not specifically authorized by Silicon Graphics is strictly
- * prohibited.
- *
- * RESTRICTED RIGHTS LEGEND:
- *
- * Use, duplication or disclosure by the Government is subject to
- * restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
- * Technical Data and Computer Software clause at DFARS 52.227-7013,
- * and/or in similar or successor clauses in the FAR, DOD or NASA FAR
- * Supplement. Unpublished - rights reserved under the Copyright Laws of
- * the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
- * Shoreline Blvd., Mountain View, CA 94039-7311
- **************************************************************************
- *
- * File: impshow.c
- *
- * Description: Displays an SGI Image file on the screen. This is a
- * simplified display program in that it requires RGB mode be
- * available.
- *
- * Usage: impshow [-o offset] [-c cacheMode] image
- *
- * Where:
- *
- * -o Specifies an offset into the file where the SGI image
- * starts. Default is 0.
- *
- * -c Specifies what form of image caching is to be used.
- * cacheMode is one of none, heap, map for no caching,
- * caching on the heap or memory mapped caching respectively.
- * Default is none.
- *
- **************************************************************************/
-
-
- #ident "$Revision: 1.2 $"
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <gl/gl.h>
- #include <gl/device.h>
- #include "imp.h"
-
-
- /* Local functions */
-
- static void usage(char *progName);
- static int drawImage(char *progName, IMPImage *image);
-
-
- /**************************************************************************
- *
- * Function: main
- *
- * Description: Program entry point.
- *
- * Parameters:
- * argc (I) - number of command line arguments
- * argv (I) - command line arguments
- *
- * Return: 0 if no errors. 1 if errors.
- *
- **************************************************************************/
-
- int main(int argc, char *argv[])
- {
- short val;
- char *pname;
- IMPImage *image;
- int ch;
- off_t offset = 0;
- IMPCacheMode cache = IMPNoCache;
-
- /*
- * Extract the program basename
- */
- pname = ((pname = strrchr(argv[0], '/')) == NULL) ? argv[0]: pname + 1;
-
- /*
- * Determine if this machine can run 24 bit RGB
- */
- if (getgdesc(GD_BITS_NORM_SNG_RED) < 8) {
- fprintf(stderr, "%s: 24-bit graphics system required\n", pname);
- exit(1);
- }
-
- /*
- * Handle command line options
- */
- while ((ch = getopt(argc, argv, "o:c:")) != EOF) {
- switch (ch) {
- case 'o':
- offset = atoi(optarg);
- break;
- case 'c':
- if (!strcasecmp(optarg, "none"))
- cache = IMPNoCache;
- else if (!strcasecmp(optarg, "heap"))
- cache = IMPHeapCache;
- else if (!strcasecmp(optarg, "map"))
- cache = IMPMapCache;
- else
- fprintf(stderr,
- "%s: unrecognized cache mode %s, none used\n",
- pname, optarg);
- break;
- case '?':
- default:
- usage(pname);
- exit(1);
- }
- }
-
- /*
- * Check the argument count
- */
- if (argc - optind < 1) {
- usage(pname);
- exit(1);
- }
-
- /*
- * Open the iamge
- */
- if ((image = impOpenExt(argv[optind], "r", offset, cache)) == NULL ) {
- impPerror(pname);
- exit(1);
- }
-
- /*
- * We cannot accomodate images larger than the screen
- */
- if (impXSize(image) > getgdesc(GD_XPMAX) ||
- impYSize(image) > getgdesc(GD_YPMAX)) {
- fprintf(stderr, "%s: input image too large - larger than %d by %d\n",
- impXSize(image), impYSize(image));
- exit(1);
- }
-
- /*
- * Create a window and set the graphics mode
- */
- prefsize(impXSize(image), impYSize(image));
- winopen(pname);
- RGBmode();
- gconfig();
-
- /*
- * Draw the image and redraw it when necessary
- */
- if (drawImage(pname, image) < 0)
- exit(1);
- while(1) {
- if (qread(&val) == REDRAW) {
- if (drawImage(pname, image) < 0)
- exit(1);
- }
- }
-
- /*
- * Close the image
- */
- if (impClose(image) < 0) {
- impPerror(pname);
- exit(1);
- }
-
- return 0;
- }
-
-
- /*
- ==========================================================================
- LOCAL FUNCTIONS
- ==========================================================================
- */
-
-
- /**************************************************************************
- *
- * Function: usage
- *
- * Description: Prints the program usage to stderr.
- *
- * Parameters:
- * progName (I) - name of program
- *
- * Return: none
- *
- **************************************************************************/
-
- static void usage(char *progName)
- {
- fprintf(stderr, "Usage: %s [-o offset] [-c cacheMode] image\n", progName);
- }
-
-
- /**************************************************************************
- *
- * Function: drawImage
- *
- * Description: Displays the image
- *
- * Parameters:
- * progName (I) - program name
- * image (I) - image to be displayed
- *
- * Return: 0 if no errors. -1 if errors.
- *
- **************************************************************************/
-
- static int drawImage(char *progName, IMPImage *image)
- {
- register int xsize = impXSize(image);
- register int ysize = impYSize(image);
- register int nchan = impNumChannels(image);
- register int y;
- int off, div = 0;
- short *rs, *gs, *bs;
-
- /*
- * Allocate image row storage
- */
- rs = (short*)malloc(xsize * sizeof(short));
- gs = (short*)malloc(xsize * sizeof(short));
- bs = (short*)malloc(xsize * sizeof(short));
- if (rs == NULL || gs == NULL || bs == NULL) {
- fprintf(stderr, "%s: memory allocation failed\n", progName);
- return -1;
- }
-
- /*
- * If the image is 2 bytes per pixel, determine if
- * scaling is needed and if so calculate the scale
- * factor
- */
- if (impRasterBPP(image) > 1 &&
- (impMaxValue(image) > 255 || impMinValue(image) < 0)) {
- off = impMinValue(image);
- div = (int)(((float)impMaxValue(image) - (float)off) / 255.0 + 0.5);
- }
-
- /*
- * Draw the image
- */
- reshapeviewport();
- viewport(0, xsize-1, 0, ysize-1);
- ortho2(-0.5, (float)xsize - 0.5, -0.5, (float)ysize - 0.5);
- if (nchan < 3) {
- for (y = 0; y < ysize; y++) {
- if (div) {
- if (impReadRow(image, rs, y, 0) < 0) {
- impPerror(progName);
- return -1;
- }
- impSDivRow(rs, rs, div, xsize);
- impPackRow((uchar_t*)rs, rs, xsize);
- } else {
- if (impReadRowB(image, (uchar_t*)rs, y, 0) < 0) {
- impPerror(progName);
- return -1;
- }
- }
- cmov2i(0, y);
- writeRGB(xsize, (uchar_t*)rs, (uchar_t*)rs, (uchar_t*)rs);
- }
- } else {
- for (y = 0; y < ysize; y++) {
- if (div) {
- if (impReadRow(image, rs, y, 0) < 0) {
- impPerror(progName);
- return -1;
- }
- if (impReadRow(image, gs, y, 1) < 0) {
- impPerror(progName);
- return -1;
- }
- if (impReadRow(image, bs, y, 2) < 0) {
- impPerror(progName);
- return -1;
- }
- impSAddRow(rs, rs, off, xsize);
- impSAddRow(gs, gs, off, xsize);
- impSAddRow(bs, bs, off, xsize);
- impSDivRow(rs, rs, div, xsize);
- impSDivRow(gs, gs, div, xsize);
- impSDivRow(bs, bs, div, xsize);
- impPackRow((uchar_t*)rs, rs, xsize);
- impPackRow((uchar_t*)gs, gs, xsize);
- impPackRow((uchar_t*)bs, bs, xsize);
- } else {
- if (impReadRowB(image, (uchar_t*)rs, y, 0) < 0) {
- impPerror(progName);
- return -1;
- }
- if (impReadRowB(image, (uchar_t*)gs, y, 1) < 0) {
- impPerror(progName);
- return -1;
- }
- if (impReadRowB(image, (uchar_t*)bs, y, 2) < 0) {
- impPerror(progName);
- return -1;
- }
- }
- cmov2i(0, y);
- writeRGB(xsize, (uchar_t*)rs, (uchar_t*)gs, (uchar_t*)bs);
- }
- }
-
- /*
- * Free the row storage
- */
- free(rs);
- free(gs);
- free(bs);
-
- return 0;
- }
-